package borge.web.teamspirit.client.page.shared.widgets;

import borge.web.teamspirit.client.SessionData;
import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.TeamSpiritConstants;
import borge.web.teamspirit.client.data.Person;
import borge.web.teamspirit.client.data.TeamPlayer;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.PasswordTextBox;

public class CreatePersonWidget extends CreateDataWidget implements ClickHandler {
	
	private Person iPerson;
    
	public CreatePersonWidget(WidgetEventHandler aEventHandler){
		super(aEventHandler);
	
		
		iPanel.add(iTable);
		initWidget(iPanel);
	}
	
	private void createDataTable(){

		TeamSpiritConstants lsc = TeamSpirit.getConstants();
		int row = 0;
		final int maxLength = 30;
		
		if(iPerson == null)
			iPerson = new Person();
		
		addTableRow(row++, new InputWrapper(lsc.username(), createTextBox(iPerson.getUserName(), maxLength), true));
		addTableRow(row++, new InputWrapper(lsc.password(), createPasswordBox(iPerson.getPassword(), maxLength), true));
		addTableRow(row++, new InputWrapper(lsc.confirm_password(), createPasswordBox(iPerson.getPassword(), maxLength), true));
		

		addTableRow(row++, new InputWrapper(lsc.first_name(), createTextBox(iPerson.getFirstName(), maxLength), true));
		addTableRow(row++, new InputWrapper(lsc.last_name(), createTextBox(iPerson.getLastName(), maxLength), true));
		addTableRow(row++, new InputWrapper(lsc.nick_name(), createTextBox(iPerson.getNickName(), maxLength), false));
		addTableRow(row++, new InputWrapper(lsc.day_of_birth(), createDateBox(iPerson.getBirthDay()), true));
		addTableRow(row++, new InputWrapper(lsc.phone(), createTextBox(iPerson.getPhone(), maxLength), true));
		addTableRow(row++, new InputWrapper(lsc.email(), createTextBox(iPerson.getEmail(), maxLength), true));
		
		
		addButton(row, 0, this);
	}
	
    private PasswordTextBox createPasswordBox(String aText, int aMaxLength) {

        PasswordTextBox box = new PasswordTextBox();
        if (aText != null)
        	
            box.setText(aText);

        box.setMaxLength(aMaxLength);

        return box;

    }
    
    @SuppressWarnings("unchecked")
    public Person getDataObject(){
        return iPerson;
    }
	
	public void onClick(ClickEvent aEvent){
		
	    // Verify input data
	    // * make sure
	    
		postEvent(WidgetEventType.PERSON_CREATED);
		
	}

	@Override
	public void updateData() {
		
		iPerson = (Person)SessionData.sEditObject;
		SessionData.sEditObject = null;
		createDataTable();
	}
	
}
